#include "dialog_new_module.h"
#include "ui_dialog_new_module.h"

#include <QDebug>
#include <QDoubleValidator>
#include <QString>

#include "projectFileConfig/projectfileconfig.h"

#if (QT_VERSION >= QT_VERSION_CHECK(5,13,0))
#include <QRegularExpressionValidator>
#endif

dialog_new_module::dialog_new_module(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dialog_new_module)
{
    ui->setupUi(this);
#if (QT_VERSION <= QT_VERSION_CHECK(5,13,0))

    ui->lineEdit_input_port->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
    ui->lineEdit_output_port->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
    ui->lineEdit_inout_port->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
#else //qt6版本
    /// 设置输入限制，只能输入数字 macos
    ui->lineEdit_input_port->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]+$")));
    ui->lineEdit_output_port->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]+$")));
    ui->lineEdit_inout_port->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]+$")));
    ///---------------------------------------------------------------
#endif

    Qt::WindowFlags winFlag = Qt::Dialog;
    winFlag |= Qt::WindowCloseButtonHint;

    setWindowFlags(winFlag);  // 设置右上角窗口按钮
}

dialog_new_module::~dialog_new_module()
{
    delete ui;
}

QString dialog_new_module::get_name()
{
    QString name = ui->lineEdit_module_name->text();

    if (name.isEmpty() || name.isNull())
        name = "New Module";

    return name;
}

int dialog_new_module::get_inout(inouts i)
{
    switch (i) {
    case inouts::input :
        return ui->lineEdit_input_port->text().toInt();
        break;
    case inouts::output :
        return ui->lineEdit_output_port->text().toInt();
        break;
    case inouts::inout :
        return ui->lineEdit_inout_port->text().toInt();
        break;
    }
    return 0;
}


void dialog_new_module::on_pushButton_cancel_clicked()
{
    reject();
}


void dialog_new_module::on_pushButton_ok_clicked()
{
    int ret = judge_port_number();
    if (ret == 0)
    {
        Module_Data data;
        int num = 0;

        data.set_module_name(this->get_name());

        for (int i(0); i < this->get_inout(inouts::input); i++) {
            PortAttribute pa;

            pa.name = "P" + QString::number(num);
//            pa.id = moduleFileConfig::makeID();
            pa.id = projectFileConfig::makeID();
            pa.type = "Input";

            data.set_input(pa);

            num++;
        }

        for (int i(0); i < this->get_inout(inouts::output); i++) {
            PortAttribute pa;

            pa.name = "P" + QString::number(num);
//            pa.id = moduleFileConfig::makeID();
            pa.id = projectFileConfig::makeID();
            pa.type = "Output";

            data.set_output(pa);

            num++;
        }

        for (int i(0); i < this->get_inout(inouts::inout); i++) {
            PortAttribute pa;

            pa.name = "P" + QString::number(num);
//            pa.id = moduleFileConfig::makeID();
            pa.id = projectFileConfig::makeID();
            pa.type = "InOut";

            data.set_inout(pa);

            num++;
        }

        emit send_Data(data);
        accept();
    }
    else
    {
        if (ret & INPUT_NUMBER)
        {
            ui->lineEdit_input_port->setStyleSheet("QLineEdit{border: 2px solid red}");
        }
        if (ret & OUTPUT_NUMBER)
        {
            ui->lineEdit_output_port->setStyleSheet("QLineEdit{border: 2px solid red}");
        }
        if (ret & INOUT_NUMBER)
        {
            ui->lineEdit_inout_port->setStyleSheet("QLineEdit{border: 2px solid red}");
        }

        ui->label_error->setText("数量不能超过10个");
    }

    return;
}

int dialog_new_module::judge_port_number()
{
    int n = 0;

    if (ui->lineEdit_input_port->text().toInt() >= MAX_PORT_NUMBER)
    {
        n  += INPUT_NUMBER;
    }
    if (ui->lineEdit_output_port->text().toInt() >= MAX_PORT_NUMBER)
    {
        n += OUTPUT_NUMBER;
    }
    if (ui->lineEdit_inout_port->text().toInt() >= MAX_PORT_NUMBER)
    {
        n += INOUT_NUMBER;
    }

    return n;
}


void dialog_new_module::on_lineEdit_input_port_textChanged(const QString &arg1)
{
    ui->lineEdit_input_port->setStyleSheet("");
}


void dialog_new_module::on_lineEdit_output_port_textChanged(const QString &arg1)
{
    ui->lineEdit_output_port->setStyleSheet("");
}


void dialog_new_module::on_lineEdit_inout_port_textChanged(const QString &arg1)
{
    ui->lineEdit_inout_port->setStyleSheet("");
}

